﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;
using System.Windows.Forms;
using LanSunStudio.ExClassLib.MessageExtention;
namespace LanSunStudio.ExClassLib
{
    [Serializable]
    public class ExMessageQueue
    {
        public MessageQueue MQInstance;
        public ExMessageQueue()
        {
            string queueName = @".\private$\CenterMQ";
            //Connect to the queue
            if (!MessageQueue.Exists(queueName))
            {
                MessageQueue.Create(queueName);
            }

            MQInstance = new MessageQueue(queueName);
        }
        public ExMessageQueue(string MQName)
        {
            if (!MessageQueue.Exists(MQName))
            {
                MessageQueue.Create(MQName);
            }
            MQInstance = new MessageQueue(MQName);
        }
        public void StartMQService()
        {
            MQInstance.BeginReceive();
        }
        public void StopMQService()
        {
            MQInstance.Purge();
            MQInstance.Close();
        }
        public void SendMessage(MessageEx MQMsgRequest)
        {
            try
            {
                ExMessageQueue MQSender =null;//= new ExMessageQueue(MQMsgRequest.MQSenderPath);
                ExMessageQueue MQReceiver = null;//new ExMessageQueue(MQMsgRequest.MQReceiverPath);
                MessageEx MsgClone= new MessageEx(MQMsgRequest);
                MQReceiver.MQInstance.Send(MsgClone);
            }
            catch (ArgumentException e)
            {
                MessageBox.Show(e.Message);
            }
            return;
        }
    }
}
